//1、读取文件
//1、引入http
const http = require('http')
//引入fs模块
const fs = require('fs')
//2、声明参数
let ip = '127.0.0.1'
let port = 9999
//3、创建服务对象
let server = http.createServer(function(request, response){
    //设置文件路径
    let filePath = "../workspace/node-day02/static/test.txt"
    console.log(filePath);

    //3-1、设置允许跨域访问
    // response.setHeader('Access-Control-Allow-Origin', '*')
    //3-2、解析request对象
    let url = request.url
    console.log(url);
    //3-3、设置全局的响应头
    response.setHeader('Content-type','text/plain;charset=utf-8')
    //读取文件
    fs.readFile("static/test.txt", 'utf-8' , function(e,res){
        console.log(res);
    //3-4、响应信息
    response.write(res)
    //3-5、终止响应，此函数后面不能再写其它代码，否则服务端报错
    response.end()
    })
})
//4、启动服务，监听请求
server.listen(port, ip, () => {
    console.log(`Server is running on http://${ip}:${port}`);
    
})
